package mobi.weq.servlets.data;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import mobi.weq.dao.PollutionData;
import mobi.weq.model.SensorObservation;
import mobi.weq.model.vo.GridVo;
import mobi.weq.util.map.Bounds;

import com.google.gson.Gson;

/**
 * Servlet implementation class RegisterUser
 */
@WebServlet("/data/currentsensorforbounds")
public class GetCurrentSensorDataForBounds extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Gson g = new Gson();
		String sw = request.getParameter("southwest");
		String ne = request.getParameter("northeast");
		Bounds bounds = new Bounds(ne, sw);
		List<SensorObservation> obs = null;
		try {
			obs = PollutionData.getCurrentGridsAndObservationForPosition(bounds, new Date());
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Gson gson = new Gson();
		String json = gson.toJson(new GridVo(obs));
		response.getWriter().print(json);
	}

}
